<?php
	require "../banco/ConexaoBanco.php";
	
	function convertDataEmAmd($dt)
	{
		$dia = substr($dt,0,2);
		$mes = substr($dt,3,2);
		$ano = substr($dt,6,4);
		$data = $ano."-".$mes."-".$dia;
		return $data;
	}
	function convertDataEmDMA($dt)
	{
		$dia = substr($dt,8,2);
		$mes = substr($dt,5,2);
		$ano = substr($dt,0,4);
		$data = $dia."/".$mes."/".$ano;
		return $data;
	}
	if (isset($_POST['de']) && isset($_POST['ate'])) {
		$de = $_POST['de'];
		$ate = $_POST['ate'];
	}
	
	
	$sql = mysql_query("SELECT COUNT(id_agendamento) AS total FROM agendamento WHERE  ag_status = 'Ativo' AND ag_data BETWEEN '".convertDataEmAmd($de)."' AND '".convertDataEmAmd($ate)."'" )or die(mysql_error());
	$res = mysql_fetch_array($sql);
	
	$sql2 = mysql_query("SELECT COUNT(ag_tipo_consulta) AS tipoC FROM agendamento WHERE  ag_status = 'Ativo' AND ag_tipo_consulta = 'Consulta' AND ag_data BETWEEN '".convertDataEmAmd($de)."' AND '".convertDataEmAmd($ate)."'" )or die(mysql_error());
	$res2 = mysql_fetch_array($sql2);
	
	$sql3 = mysql_query("SELECT COUNT(ag_tipo_consulta) AS tipoE FROM agendamento WHERE  ag_status = 'Ativo' AND ag_tipo_consulta = 'Exame' AND ag_data BETWEEN '".convertDataEmAmd($de)."' AND '".convertDataEmAmd($ate)."'" )or die(mysql_error());
	$res3 = mysql_fetch_array($sql3);
	
	$sql4 = mysql_query("SELECT COUNT(ag_tipo_consulta) AS tipoX FROM agendamento WHERE  ag_status = 'Ativo' AND ag_tipo_consulta = 'Raio-X' AND ag_data BETWEEN '".convertDataEmAmd($de)."' AND '".convertDataEmAmd($ate)."'" )or die(mysql_error());
	$res4 = mysql_fetch_array($sql4);
	
	$sql5 = mysql_query("SELECT COUNT(id_agendamento) AS total FROM agendamento WHERE  ag_status = 'Cancelado' AND ag_data BETWEEN '".convertDataEmAmd($de)."' AND '".convertDataEmAmd($ate)."'" )or die(mysql_error());
	$res5 = mysql_fetch_array($sql5);
	
	$sql6 = mysql_query("SELECT COUNT(id_paciente) AS total FROM paciente WHERE  p_status = 1 AND p_data_cadastro BETWEEN '".convertDataEmAmd($de)."' AND '".convertDataEmAmd($ate)."'" )or die(mysql_error());
	$res6 = mysql_fetch_array($sql6);
	
	require_once "../fpdf/fpdf.php";
	
	$pdf= new FPDF("P","pt","A4");
	
	$pdf->AddPage();
	
	$pdf->Ln(30);
	$pdf->SetFont('Times','B',14);
	$pdf->Cell(0,20,utf8_decode("Relatório de Consultas"),0,1,'C');
	$pdf->Cell(0,5,"","B",1,'C');
	$pdf->Ln(20);
	
	$pdf->SetFont('Times','B',12);
	$pdf->Cell(0,20,utf8_decode("Dados do perído de $de até $ate"),0,1,'L');
	$pdf->Ln(20);
	
	// total consultas
	$pdf->SetFont('Times','',12);
	$pdf->Cell(180,20,"Total de pacientes consultados:",0,0,'L');
	$pdf->setFont('Times','',12);
	$pdf->Cell(0,20,$res['total'],0,1,'L');
	$pdf->Ln(8);
	
	// total por tipos de consulta
	$pdf->SetFont('Times','',12);
	$pdf->Cell(180,20,utf8_decode("Consultas mais agendadas"),0,1,'L');
	$pdf->Cell(180,20,utf8_decode("					Consultas Geral:"),0,0,'L');
	$pdf->setFont('Times','',12);
	if($res2['tipoC']==null){
		$pdf->Cell(0,20,'0',0,1,'L');
	}else{
		$pdf->Cell(0,20,$res2['tipoC'],0,1,'L');
	}
	$pdf->Cell(180,20,utf8_decode("					Exames:"),0,0,'L');
	if($res3['tipoE']==null){
		$pdf->Cell(0,20,'0',0,1,'L');
	}else{
		$pdf->Cell(0,20,$res3['tipoE'],0,1,'L');
	}
	$pdf->Cell(180,20,utf8_decode("					Raio-X:"),0,0,'L');
	if($res4['tipoX']==null){
		$pdf->Cell(0,20,'0',0,1,'L');
	}else{
		$pdf->Cell(0,20,$res4['tipoX'],0,1,'L');
	}
	$pdf->Ln(8);
	
	$pdf->SetFont('Times','',12);
	$pdf->Cell(180,20,"Total de consultas canceladas:",0,0,'L');
	$pdf->setFont('Times','',12);
	$pdf->Cell(0,20,$res5['total'],0,1,'L');
	$pdf->Ln(8);
	
	$pdf->SetFont('Times','',12);
	$pdf->Cell(220,20,utf8_decode("Total de pacientes cadastrados no período: "),0,0,'L');
	$pdf->setFont('Times','',12);
	$pdf->Cell(0,20,$res6['total'],0,1,'L');
	$pdf->Ln(8);
	
	$pdf->Output();
	
?>
